package mysql

import (
	Config "fileService/config"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var conn *gorm.DB

func init()  {
	conn=connect()
}
func connect() *gorm.DB {
	dsn :=fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",Config.Config.DateBase.Username,Config.Config.DateBase.Password,Config.Config.DateBase.Host,Config.Config.DateBase.Port,Config.Config.DateBase.Database)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err !=nil{
		log.Fatal(err.Error())
	}
	return db
}

func DB() *gorm.DB {
	if conn==nil{
		conn=connect()
	}
	return conn
}